package com.lagou.rabbithomework.demo.controller;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;



@Controller
public class PayController {
    @Autowired
    private AmqpTemplate rabbitmqTemplate;

    /**
     * 发送订单，等待支付
     * @return
     */
    @RequestMapping("/pay")
    public String distributeGo(String productname,Integer price,Model model){
        rabbitmqTemplate.convertAndSend("ex.pay","pay","商品名:"+productname+",单价:"+price+"元");
        model.addAttribute("productname",productname);
        model.addAttribute("price",price);
        return "waitpay";
    }

    /**
     * 完成支付订单
     * @return
     */
    @RequestMapping("/payorder")
    public String payOrder(Model model){
        String payOrder = (String)rabbitmqTemplate.receiveAndConvert("queue.pay");
        model.addAttribute("payOrder" , payOrder);
        return "success";
    }


    /**
     * 死信队列中的消息
     * @return
     */
    @RequestMapping("/notpay")
    @ResponseBody
    public String getNoPay(Model model) {
    String notPay = (String)rabbitmqTemplate.receiveAndConvert("queue.pay.dlx");
    model.addAttribute("notPay",notPay);
    return "超时未支付的订单信息:" + notPay;
    }
}
